let mongoose = require('mongoose');
let ObjectId = mongoose.Schema.Types.ObjectId;
const conn = mongoose.createConnection('mongodb://localhost:27017/xx',{ useNewUrlParser: true });
let UserSchema = new mongoose.Schema({
    username:{type:String,required:true},
    password:String,
    age:Number,
    phone:String,
    province:String,
    city:String,
    createAt:{type:Date,default:Date.now}
});
UserSchema.virtual('area').get(function(){
    return this.phone.split('-')[0];
});
UserSchema.virtual('number').get(function(){
    return this.phone.split('-')[1];
});
UserSchema.virtual('address').get(function(){
    return this.province+this.city;
});
//什么虚拟属性  010-6255888 
let User = conn.model('User',UserSchema);
let user = new User({username:'张三',phone:'010-6255888' });
//let area = user.phone.split('-')[0];
//let number = user.phone.split('-')[1];
let area = user.area;
let number = user.number;
console.log(area,number);

